package com.manager.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manager.mapper.TbCommodityMapper;
import com.manager.mapper.TbInlibraryMapper;
import com.manager.pojo.TbCommodity;
import com.manager.pojo.TbCommodityExample;
import com.manager.pojo.TbInlibrary;
import com.manager.pojo.TbInlibraryExample;
import com.manager.pojo.TbInlibraryExample.Criteria;
import com.manager.service.InLibraryService;
import com.manager.vo.TbUserEx;

@Service
public class InLibraryServiceimpl implements InLibraryService {

	@Autowired
	private TbInlibraryMapper tbInlibraryMapper;
	
	@Autowired
	private TbCommodityMapper tbCommodityMapper;
	
	@Override		//添加到入库
	public TbUserEx addInLibrary(TbInlibrary tbInlibrary) {
		
		TbUserEx tbUserEx = new TbUserEx();
		Integer result = 0;
		
		try {
			result = tbInlibraryMapper.insertSelective(tbInlibrary);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("入库列表获取异常");
			return tbUserEx;
		}
		
		if(result<0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("添加入库失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setError("添加入库成功");
		
		return tbUserEx;
	}

	@Override			//根据ID从入库中删除
	public TbUserEx delnLibrary(Integer id) {
		
		TbUserEx tbUserEx = new TbUserEx();
		Integer result = 0;
		
		try {
			result = tbInlibraryMapper.deleteByPrimaryKey(id);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("入库记录删除异常");
			return tbUserEx;
		}
		
		if(result<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("入库记录删除失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("入库记录删除成功");
		
		return tbUserEx;
	}

	@Override			//根据主键更新入库
	public TbUserEx editInLibrary(TbInlibrary tbInlibrary) {
		
		TbUserEx tbUserEx = new TbUserEx();
		Integer result  = 0;
		try {
			result = tbInlibraryMapper.updateByPrimaryKeySelective(tbInlibrary);
		}catch(Exception e) {
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("入库记录编辑异常");
			return tbUserEx;
		}
		
		if(result<=0) {
			tbUserEx.setCode(400);
			tbUserEx.setError("入库记录编辑失败");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("入库记录编辑成功");
		
		return tbUserEx;
	}

	@Override			//入库的数量不等于0的情况下  根据ID倒叙排序返回结果
	public TbUserEx getInLibraryList(Integer page, Integer rows) {
		
		TbUserEx tbUserEx = new TbUserEx();
		
		TbInlibraryExample example = new TbInlibraryExample();
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andInlibraryNotEqualTo(0);
		//分页范围设置
		PageHelper.startPage(page,rows);
		
		List<TbInlibrary>list = null;
		
		try {
			list = tbInlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("入库列表获取异常");
			return tbUserEx;
		}
		
		if(list.size()<=0 || list == null) {
			
			tbUserEx.setCode(500);
			tbUserEx.setError("入库列表没有信息");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("入库列表获取成功");
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		
		return tbUserEx;
	}
	
	

	
	@Override		//根据入库商品名称查询入库商品返回
	public TbInlibrary getInLibraryName(TbInlibrary tbInlibrary) throws Exception {
		
		List<TbInlibrary>list = null;
		
		TbInlibraryExample example = new TbInlibraryExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(tbInlibrary.getName());
		
		try {
			list = tbInlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
			throw new Exception();
		}
		
		if(list.size()<=0) {
			return null;
		}
		
		
		return list.get(0);
	}

	@Override		//根据id查入库记录
	public TbInlibrary getTbInlibraryById(Integer id) throws Exception {

		TbInlibrary tbInlibrary = null;
		
		try {
			tbInlibrary =  tbInlibraryMapper.selectByPrimaryKey(id);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return tbInlibrary!=null?tbInlibrary:null;
	}

	@Override			//删除入库等于0的 记录
	public Boolean delnLibraryEqualZero() {
		
		TbInlibraryExample example = new TbInlibraryExample();
		Criteria criteria = example.createCriteria();
		criteria.andInlibraryEqualTo(0);

		Integer result = 0;
		try {
			result = tbInlibraryMapper.deleteByExample(example);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return result>0?true:false;
	}

	@Override			//根据时间范围查库存
	public TbUserEx getInLibraryListByDateTime(Integer page, Integer rows, Date start, Date end) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbInlibraryExample example = new TbInlibraryExample();
		List<TbInlibrary>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andDtimeBetween(start, end);
		//分页范围设置
		PageHelper.startPage(page, rows);
		
		try {
			list = tbInlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按时间查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			
			tbUserEx.setCode(400);
			tbUserEx.setError("按时间查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按时间查询成功");
		
		return tbUserEx;
	}

	@Override		//按商品名称查询
	public TbUserEx getInLibraryListByName(Integer page, Integer rows, String like) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbInlibraryExample example = new TbInlibraryExample();
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(like);
		//分页范围设置
		PageHelper.startPage(page, rows);
		
		List<TbInlibrary>list = null;
		
		try {
			list = tbInlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按商品名称查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			
			tbUserEx.setCode(400);
			tbUserEx.setError("按商品名称查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按商品名称查询成功");
		
		return tbUserEx;
	}

	@Override				//根据查询的类型来获取入库的分页信息
	public TbUserEx getInLibraryListByType(Integer page, Integer rows, String like) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbInlibraryExample example = new TbInlibraryExample();
		List<TbInlibrary>list = null;
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andTypeEqualTo(like);
		//分页范围设置
		PageHelper.startPage(page, rows);
		
		try {
			list = tbInlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按商品分类查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			
			tbUserEx.setCode(400);
			tbUserEx.setError("按商品分类查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按商品分类查询成功");
		
		return tbUserEx;
	}

	@Override			//根据SKU查询入库分页信息
	public TbUserEx getInLibraryListBySKU(Integer page, Integer rows, String like) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbInlibraryExample example = new TbInlibraryExample();
		
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		criteria.andSkuEqualTo(like);
		//分页范围设置
		PageHelper.startPage(page, rows);
		
		List<TbInlibrary>list = null;
		
		try {
			list = tbInlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("按商品SKU查询异常");
			return tbUserEx;
		}
		
		if(list==null || list.size()<=0) {
			
			tbUserEx.setCode(400);
			tbUserEx.setError("按商品SKU查询失败");
			return tbUserEx;
		}
		
		PageInfo pageinfo = new PageInfo(list);
		tbUserEx.setPageinfo(pageinfo);
		tbUserEx.setRows(list);
		tbUserEx.setCode(200);
		tbUserEx.setMsg("按商品SKU查询成功");
		
		return tbUserEx;
	}

	@Override			//根据时间 获取入库分页信息
	public TbUserEx getInLibraryList(Date start,Date end) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbInlibraryExample example = new TbInlibraryExample();
		
		example.setOrderByClause("id  desc");
		Criteria criteria = example.createCriteria();
		criteria.andInlibraryNotEqualTo(0);
		criteria.andDtimeBetween(start, end);
		
		List<TbInlibrary>list = null;
		
		try {
			list = tbInlibraryMapper.selectByExample(example);
		}catch(Exception e) {
			
			e.printStackTrace();
			tbUserEx.setCode(500);
			tbUserEx.setError("入库列表获取异常");
			return tbUserEx;
		}
		
		if(list.size()<=0 || list == null) {
			
			tbUserEx.setCode(500);
			tbUserEx.setError("入库列表没有信息");
			return tbUserEx;
		}
		
		tbUserEx.setCode(200);
		tbUserEx.setMsg("入库列表获取成功");
		tbUserEx.setRows(list);
		
		return tbUserEx;
	}
}
